package com.example.havi.util;


import android.util.Log;

import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMGroupManager;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMGroupMemberRoleType;
import com.tencent.imsdk.TIMValueCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装环信 sdk 的方法
 */
public class ApiUtil {

    /**
     * 创建群组
     * @param groupName
     * @param desc
     * @param allMembers
     * @param image
     */
    public static void asyncCreateGroup(String groupName,String desc,String[] allMembers,String image){
        //创建公开群，且不自定义群 ID
        TIMGroupManager.CreateGroupParam param = new TIMGroupManager.CreateGroupParam("Public", "test_group");
//指定群简介
        param.setIntroduction("hello world");
//指定群公告
        param.setNotification("welcome to our group");

//添加群成员

        List<TIMGroupMemberInfo> infos = new ArrayList<TIMGroupMemberInfo>();
        for (String memberName:allMembers)
        {
            TIMGroupMemberInfo member = new TIMGroupMemberInfo(memberName);
            member.setRoleType(TIMGroupMemberRoleType.Normal);
            infos.add(member);
            param.setMembers(infos);
        }

//创建群组
        TIMGroupManager.getInstance().createGroup(param, new TIMValueCallBack<String>() {
            @Override
            public void onError(int code, String desc) {
                Log.d("mango", "create group failed. code: " + code + " errmsg: " + desc);
            }

            @Override
            public void onSuccess(String s) {
                Log.d("mango", "create group succ, groupId:" + s);
            }
        });

    }

    /**
     * 解散群 只有创建者才可以解散
     * @param groupId
     */
    public static void asyncDestroyGroup(String groupId) {


        //解散群组
        TIMGroupManager.getInstance().deleteGroup(groupId, new TIMCallBack() {
            @Override
            public void onError(int code, String desc) {

                //错误码 code 和错误描述 desc，可用于定位请求失败原因
                //错误码 code 列表请参见错误码表
                LogUtil.e("mango", " 解散群失败" + code + " errmsg: " + desc);
            }

            @Override
            public void onSuccess() {
                //解散群组成功
                LogUtil.e("mango", " 解散群");
            }

        });
    }
}
